package com.ruoyi.project.gzbook.indexmenu.controller;

import java.util.List;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.gzbook.indexmenu.domain.DgIndexMenu;
import com.ruoyi.project.gzbook.indexmenu.service.IDgIndexMenuService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.framework.web.domain.Ztree;

/**
 * 图书分类类目Controller
 * 
 * @author mhg
 * @date 2021-10-08
 */
@Controller
@RequestMapping("/gzbook/indexmenu")
public class DgIndexMenuController extends BaseController
{
    private String prefix = "gzbook/indexmenu";

    @Autowired
    private IDgIndexMenuService dgIndexMenuService;

    @RequiresPermissions("gzbook:indexmenu:view")
    @GetMapping()
    public String indexmenu()
    {
        return prefix + "/indexmenu";
    }

    /**
     * 查询图书分类类目树列表
     */
    @RequiresPermissions("gzbook:indexmenu:list")
    @PostMapping("/list")
    @ResponseBody
    public List<DgIndexMenu> list(DgIndexMenu dgIndexMenu)
    {
        List<DgIndexMenu> list = dgIndexMenuService.selectDgIndexMenuList(dgIndexMenu);
        return list;
    }

    /**
     * 导出图书分类类目列表
     */
    @RequiresPermissions("gzbook:indexmenu:export")
    @Log(title = "图书分类类目", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(DgIndexMenu dgIndexMenu)
    {
        List<DgIndexMenu> list = dgIndexMenuService.selectDgIndexMenuList(dgIndexMenu);
        ExcelUtil<DgIndexMenu> util = new ExcelUtil<DgIndexMenu>(DgIndexMenu.class);
        return util.exportExcel(list, "图书分类类目数据");
    }

    /**
     * 新增图书分类类目
     */
    @GetMapping(value = { "/add/{id}", "/add/" })
    public String add(@PathVariable(value = "id", required = false) Long id, ModelMap mmap)
    {
        if (StringUtils.isNotNull(id))
        {
            mmap.put("dgIndexMenu", dgIndexMenuService.selectDgIndexMenuById(id));
        }
        return prefix + "/add";
    }

    /**
     * 新增保存图书分类类目
     */
    @RequiresPermissions("gzbook:indexmenu:add")
    @Log(title = "图书分类类目", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(DgIndexMenu dgIndexMenu)
    {
        return toAjax(dgIndexMenuService.insertDgIndexMenu(dgIndexMenu));
    }

    /**
     * 修改图书分类类目
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        DgIndexMenu dgIndexMenu = dgIndexMenuService.selectDgIndexMenuById(id);
        mmap.put("dgIndexMenu", dgIndexMenu);
        return prefix + "/edit";
    }

    /**
     * 修改保存图书分类类目
     */
    @RequiresPermissions("gzbook:indexmenu:edit")
    @Log(title = "图书分类类目", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(DgIndexMenu dgIndexMenu)
    {
        return toAjax(dgIndexMenuService.updateDgIndexMenu(dgIndexMenu));
    }

    /**
     * 删除
     */
    @RequiresPermissions("gzbook:indexmenu:remove")
    @Log(title = "图书分类类目", businessType = BusinessType.DELETE)
    @GetMapping("/remove/{id}")
    @ResponseBody
    public AjaxResult remove(@PathVariable("id") Long id)
    {
        return toAjax(dgIndexMenuService.deleteDgIndexMenuById(id));
    }

    /**
     * 选择图书分类类目树
     */
    @GetMapping(value = { "/selectIndexmenuTree/{id}", "/selectIndexmenuTree/" })
    public String selectIndexmenuTree(@PathVariable(value = "id", required = false) Long id, ModelMap mmap)
    {
        if (StringUtils.isNotNull(id))
        {
            mmap.put("dgIndexMenu", dgIndexMenuService.selectDgIndexMenuById(id));
        }
        return prefix + "/tree";
    }

    /**
     * 加载图书分类类目树列表
     */
    @GetMapping("/treeData")
    @ResponseBody
    public List<Ztree> treeData()
    {
        List<Ztree> ztrees = dgIndexMenuService.selectDgIndexMenuTree();
        return ztrees;
    }
}
